﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ProjectPheonix {
    public class Player : Microsoft.Xna.Framework.GameComponent {
        public Matrix view { get; protected set; }
        public Matrix projection { get; protected set; }

        public Vector3 playerPosition { get; protected set; }
        private Vector3 playerDirection;
        private Vector3 playerUp;

        private float tempY;

        float speed = 3;

        MouseState prevMouseState;

        public Vector3 GetPlayerDirection {
            get { return playerDirection; }
        }

        public Player(Game game, Vector3 pos, Vector3 target, Vector3 up)
            : base(game) {
            playerPosition = pos;
            playerDirection = target - pos;
            playerDirection.Normalize();
            playerUp = up;
            CreateLookAt();

            projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4,
                (float)Game.Window.ClientBounds.Width /
                (float)Game.Window.ClientBounds.Height,
                1, 3000);
        }

        public override void Initialize() {
            Mouse.SetPosition(Game.Window.ClientBounds.Width / 2,
                Game.Window.ClientBounds.Height / 2);
            prevMouseState = Mouse.GetState();

            base.Initialize();
        }

        public override void Update(GameTime gameTime) {
#if WINDOWS
            tempY = playerDirection.Y;
            playerDirection.Y = 0;

            if (Keyboard.GetState().IsKeyDown(Keys.W))
                playerPosition += playerDirection * speed;
            if (Keyboard.GetState().IsKeyDown(Keys.S))
                playerPosition -= playerDirection * speed;

            // Resets Mouse X to loop around the screen \\
            if (Mouse.GetState().X >= 1300) {
                Mouse.SetPosition(201, Mouse.GetState().Y);
                prevMouseState = Mouse.GetState();
            }

            if (Mouse.GetState().X <= 200) {
                Mouse.SetPosition(1299, Mouse.GetState().Y);
                prevMouseState = Mouse.GetState();
            }
            ///////////////////////////////////////////////

            if (Keyboard.GetState().IsKeyDown(Keys.A))
                playerPosition += Vector3.Cross(playerUp, playerDirection) * speed;
            if (Keyboard.GetState().IsKeyDown(Keys.D))
                playerPosition -= Vector3.Cross(playerUp, playerDirection) * speed;

            playerDirection.Y = tempY;

            playerDirection = Vector3.Transform(playerDirection,
                Matrix.CreateFromAxisAngle(playerUp, (-MathHelper.PiOver4 / 150) *
                (Mouse.GetState().X - prevMouseState.X)));



            playerDirection = Vector3.Transform(playerDirection,
                               Matrix.CreateFromAxisAngle(Vector3.Cross(playerUp, playerDirection),
                               (MathHelper.PiOver4 / 100) *
                               (Mouse.GetState().Y - prevMouseState.Y)));



            prevMouseState = Mouse.GetState();

            CreateLookAt();
#endif
#if XBOX
#endif

            base.Update(gameTime);
        }

        private void CreateLookAt() {
            view = Matrix.CreateLookAt(playerPosition,
                playerPosition + playerDirection, playerUp);
        }
    }
}
